import { ref } from "vue";
import { CoinType, useKline } from "@/stores/kline";
const history = "Searchhistory";
const tablecoin = {
    coin: ref<string[]>([]),
    show: ref(false),
    addItem(item: CoinType) {
        if (localStorage.getItem(history)) {
            const arr: string[] = JSON.parse(localStorage.getItem(history)!);
            if (arr.includes(item.symbol)) {
                arr.splice(arr.indexOf(item.symbol), 1);
            } else {
                arr.unshift(item.symbol);
            }
            if (arr.length > 8) {
                arr.pop();
            }
            localStorage.setItem(history, JSON.stringify(arr));
        } else {
            localStorage.setItem(history, JSON.stringify([item.symbol]));
        }
        this.coin.value = JSON.parse(localStorage.getItem(history)!);
        this.isShow();
    },
    init() {
        if (JSON.parse(localStorage.getItem(history)!)) {
            this.coin.value = JSON.parse(localStorage.getItem(history)!);
        }
        this.isShow();
    },
    isShow() {
        try {
            const arr: string[] = JSON.parse(localStorage.getItem(history)!);
            if (arr.length > 0) {
                this.show.value = true;
            } else {
                this.show.value = false;
            }
        } catch (error) {
            this.show.value = false;
        }
    },
    clear() {
        localStorage.setItem(history, JSON.stringify([]));
        this.isShow();
    }
};
export { tablecoin };